<template>
  <div class="app-container">
    <div class="search-con">
      <div class="title">查询条件</div>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
        <!-- <el-form-item label="门店id" prop="iShopId">
          <el-input v-model="queryParams.iShopId" placeholder="请输入门店id" clearable @keyup.enter="handleQuery" />
        </el-form-item> -->
        <el-form-item label="省份" prop="provinceName">
          <el-select v-model="queryParams.provinceName" placeholder="请选择省份" @change="handleProvinceChange" clearable>
            <el-option v-for="district in provinceList" :key="district.provinceName" :label="district.provinceName" :value="district.provinceName" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityName">
          <el-select v-model="queryParams.cityName" placeholder="请选择城市" @change="handleCityChange" clearable>
            <el-option v-for="district in cityList" :key="district.cityName" :label="district.cityName" :value="district.cityName" />
          </el-select>
        </el-form-item>
        <el-form-item label="地区" prop="districtName">
          <el-select v-model="queryParams.districtName" placeholder="请选择地区" clearable>
            <el-option v-for="district in districtList" :key="district.districtName" :label="district.districtName" :value="district.districtName" />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
      </el-form>
      <div class="search-btn-con">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="Refresh" @click="resetQuery">重置</el-button>
      </div>
    </div>
    <div class="main-con">
      <div class="title-con">
        <div class="title">基本信息</div>
        <div class="operate-btn-con">
          <el-button @click="handleRefresh" icon="Refresh" v-hasPermi="['invest:ishop:add']">刷新门店列表</el-button>
        </div>
      </div>
      <div class="content-con" v-loading="loading">
        <el-table v-loading="loading" :data="ishopList" @selection-change="handleSelectionChange" height="calc(100% - 0.62rem)">
          <el-table-column type="index" label="序号" :index="indexMethod" width="60"></el-table-column>
          <el-table-column label="门店id" width="150" align="center" prop="ishopId" />
          <el-table-column label="省份" width="150" align="center" prop="provinceName" />
          <el-table-column label="城市" width="100" align="center" prop="cityName" />
          <el-table-column label="地区" width="100" align="center" prop="districtName" />
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="地址" align="center" prop="address" />
          <el-table-column label="经度" width="100" align="center" prop="lat" />
          <el-table-column label="纬度" width="100" align="center" prop="lng" />
          <!-- <el-table-column label="公司名称" width="220" align="center" prop="tenantName" /> -->
          <el-table-column label="创建时间" align="center" width="160" prop="createTime" />
        </el-table>
        <el-pagination
          small
          background
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup name="Ishop">
import { listIshop, refreshShop, listProvince, listCity, listDistrict } from '@/api/invest/ishop'
// eslint-disable-next-line no-unused-vars
import { require } from '@/utils/require'
const { proxy } = getCurrentInstance()

const ishopList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)

const provinceList = ref([])
const cityList = ref([])
const districtList = ref([])
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    iShopId: null,
    provinceName: null,
    cityName: null,
    districtName: null,
    province: null,
    city: null,
    district: null,
    tenantName: null,
    name: null
  },
  queryDistrictParams: {
    pageNum: 1,
    pageSize: 2000,
    provinceName: null,
    cityName: null,
    districtName: null
  }
})

const { queryParams, queryDistrictParams } = toRefs(data)

/** 查询省份列表 */
function getProvinceList() {
  listProvince(queryDistrictParams.value).then((response) => {
    provinceList.value = response.rows
  })
}

/** 查询城市列表 */
function getCityList() {
  listCity(queryDistrictParams.value).then((response) => {
    cityList.value = response.rows
  })
}

/** 查询区域列表 */
function getDistrictList() {
  listDistrict(queryDistrictParams.value).then((response) => {
    districtList.value = response.rows
  })
}

/** 查询门店列表列表 */
function getList() {
  loading.value = true
  listIshop(queryParams.value).then((response) => {
    ishopList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  queryDistrictParams.value.provinceName = null
  queryDistrictParams.value.cityName = null
  listCity(queryDistrictParams.value).then((response) => {
    cityList.value = response.rows
    listDistrict(queryDistrictParams.value).then((response) => {
      districtList.value = response.rows
    })
  })
  handleQuery()
}

// 分页
const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getList()
}

//每页显示条数改变
const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  getList()
}
// 序号翻页递增
const indexMethod = (index) => {
  const nowPage = queryParams.value.pageNum //当前第几页，根据组件取值即可
  const nowLimit = queryParams.value.pageSize //当前每页显示几条，根据组件取值即可
  return index + 1 + (nowPage - 1) * nowLimit // 这里可以理解成一个公式
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.shopId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleRefresh() {
  refreshShop().then(() => {
    getList()
    proxy.$modal.msgSuccess('刷新成功')
  })
}

const handleProvinceChange = (provinceName) => {
  queryDistrictParams.value.provinceName = provinceName
  queryDistrictParams.value.cityName = null
  queryParams.value.cityName = null
  listCity(queryDistrictParams.value).then((response) => {
    cityList.value = response.rows
    listDistrict(queryDistrictParams.value).then((response) => {
      districtList.value = response.rows
    })
  })
}

const handleCityChange = (cityName) => {
  queryDistrictParams.value.cityName = cityName
  queryDistrictParams.value.districtName = null
  queryParams.value.districtName = null
  listDistrict(queryDistrictParams.value).then((response) => {
    districtList.value = response.rows
  })
}

getList()
getProvinceList()
getCityList()
getDistrictList()
</script>
